﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace NoteSystem
{
    public partial class Info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {           
            //页面首次加载机制：不关值修改不了
            if (!IsPostBack)
            {
                if (Session["User"]!=null)
                {
                    int id = Convert.ToInt32(Request["id"]);
                    InitCity();
                    if (id!=0)
                    {
                        DataSet ds = DbHelper.Query($@"select u.*,c.CityName from users u inner join S_City c on CityID=city_id where u.id='{id}'");
                        txtAccount.Text=ds.Tables[0].Rows[0]["account"].ToString();
                        txtBirthday.Text = DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString()).ToString("yyyy-MM-dd");
                        txtPwd.Text=ds.Tables[0].Rows[0]["pwd"].ToString();      
                        DataSet das = DbHelper.Query($@"select p.* from S_Province p inner join S_City c on c.ProvinceID=p.ProvinceID where c.CityID='{ds.Tables[0].Rows[0]["city_id"]}'"); 
                        ddlProvince.SelectedValue=das.Tables[0].Rows[0]["ProvinceID"].ToString();
                        ddlCity.SelectedValue=ds.Tables[0].Rows[0]["city_id"].ToString();
                        rblGender.SelectedValue=ds.Tables[0].Rows[0]["gender"].ToString();
                        Image1.ImageUrl=ds.Tables[0].Rows[0]["img_url"].ToString();
                        txtPhone.Text=ds.Tables[0].Rows[0]["phone"].ToString();
                        txtEmail.Text=ds.Tables[0].Rows[0]["email"].ToString();
                        txtQQ.Text=ds.Tables[0].Rows[0]["qq"].ToString();
                    }
                    else
                    {

                        DataRow userRow = (DataRow)Session["User"];
                        txtAccount.Text=userRow["account"].ToString();
                        txtBirthday.Text = DateTime.Parse(userRow["birthday"].ToString()).ToString("yyyy-MM-dd");
                        txtPwd.Text=userRow["pwd"].ToString();
                        DataSet das = DbHelper.Query($@"select p.*,c.CityID from S_Province p inner join S_City c on c.ProvinceID=p.ProvinceID where c.CityID='{userRow["city_id"]}'");
                        ddlProvince.SelectedValue=das.Tables[0].Rows[0]["ProvinceID"].ToString();
                        ddlCity.SelectedValue=das.Tables[0].Rows[0]["CityID"].ToString();
                        rblGender.SelectedValue=userRow["gender"].ToString();
                        Image1.ImageUrl=userRow["img_url"].ToString();
                        txtPhone.Text=userRow["phone"].ToString();
                        txtEmail.Text=userRow["email"].ToString();
                        txtQQ.Text=userRow["qq"].ToString();
                    }
                    ddlProvince_SelectedIndexChanged(ddlProvince, EventArgs.Empty);

                }
                else
                {
                    Response.Redirect("Login.aspx");
                }
                
            }
        }

        public void InitCity()
        {
            DataSet ds = DbHelper.Query("select * from S_Province");
            ddlProvince.DataSource = ds;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceID";
            ddlProvince.DataBind();
        }
        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pid = Convert.ToInt32(ddlProvince.SelectedValue);
            DataSet ds = DbHelper.Query("select * from S_City where ProvinceID="+ pid);
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
        }

        protected void btnCommit_Click(object sender, EventArgs e)
        {
            int user_id = Convert.ToInt32(Request["id"]);
            DataRow userRow = (DataRow)Session["User"];
            int login_id = Convert.ToInt32(userRow["id"].ToString());
            string account = txtAccount.Text.Trim();
            string pwd = txtPwd.Text.Trim();
            DateTime birthday = DateTime.Parse(txtBirthday.Text.Trim());
            string imgUrl = Image1.ImageUrl;
            int city = Convert.ToInt32(ddlCity.SelectedValue);//获取下拉框的值
            int gender = Convert.ToInt32(rblGender.SelectedValue);
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq = txtQQ.Text.Trim();
            if (user_id!=0)
            {
                string sql1 = string.Format(@"update users set
             account='{0}',pwd='{1}',birthday='{2}',city_id={3},gender={4},img_url='{5}',phone='{6}',email='{7}',qq='{8}' where id='{9}'", account, pwd, birthday.ToString("yyyy-MM-dd"), city, gender, imgUrl, phone, email, qq, user_id);
                int flag1 = DbHelper.ExecuteSql(sql1);
                if (flag1>0)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                }
            }
            else
            {
                string sql = string.Format(@"update users set
             account='{0}',pwd='{1}',birthday='{2}',city_id={3},gender={4},img_url='{5}',phone='{6}',email='{7}',qq='{8}' where id='{9}'", account, pwd, birthday.ToString("yyyy-MM-dd"), city, gender, imgUrl, phone, email, qq, login_id);
                int flag = DbHelper.ExecuteSql(sql);
                if (flag>0)
                {
                    Response.Write("<script>alert('修改成功')</script>");
                }
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string exList = ".jpg.gif.png";
            //1.捕获文件名称
            string imgName = fulImg.FileName;
            string exName = imgName.Substring(imgName.LastIndexOf('.'));//截取扩展名
            if (!exList.Contains(exName))
            {
                Response.Write("<script>alert('不支持该图片格式')</script>");
                return;
            }
            if (fulImg.FileContent.Length>2*1024*1024)
            {
                Response.Write("<script>alert('图片大小不能超过1M')</script>");
                return;
            }
            //2.确定文件保存的物理地址
            string path = Server.MapPath("images")+"\\"+imgName;
            //3.进行文件保存
            fulImg.SaveAs(path);
            Image1.ImageUrl = "/images/"+imgName;
        }

       
    }
}